package com.cpr.ams.web.controller;

import com.cpr.ams.bean.BaseUser;
import com.cpr.ams.bean.Prince;
import com.cpr.ams.bean.Room;
import com.cpr.ams.bean.extend.RoomExtend;
import com.cpr.ams.service.IRoomService;
import com.cpr.ams.util.ExcelUtils;
import com.cpr.ams.util.Result;
import com.jacky.base.utils.PageVM;
import com.sun.istack.internal.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletResponse;

@Api(description = "房屋操作以及房屋类型操作相关接口")
@Validated
@RestController
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private IRoomService roomService;
    @ApiOperation(value = "插入或更新房屋")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roomId", value = "主键",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "status", value = "房屋状态",paramType = "query",required = true),
            @ApiImplicitParam(name = "roomImg", value = "房屋图片地址",paramType = "query",required = true),
            @ApiImplicitParam(name = "type", value = "房屋类型外键id",paramType = "query",required = true),
            @ApiImplicitParam(name = "backup1", value = "房屋名称",paramType = "query"),
            @ApiImplicitParam(name = "backup2", value = "房屋描述",paramType = "query")
    })
    @PostMapping("/saveOrUpdateRoom")
    public Result saveOrUpdateRoom(Integer roomId, @NotNull String status, @NotNull String roomImg, @NotNull Integer type,String backup1,String backup2){
        Room room=new Room();
        room.setRoomId(roomId);
        room.setStatus(status);
        room.setRoomImg(roomImg);
        room.setType(type);
        room.setBackup1(backup1);
        room.setBackup2(backup2);
            roomService.saveOrUpdateRoom(room);
            return Result.success("插入或更新成功");
    }
    @ApiOperation(value = "插入或更新房屋类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "description", value = "房屋类型描述",paramType = "query",required = true),
            @ApiImplicitParam(name = "day", value = "房屋日租价格",paramType = "query",required = true),
            @ApiImplicitParam(name = "month", value = "房屋月租价格",paramType = "query",required = true),
            @ApiImplicitParam(name = "year", value = "房屋年租价格",paramType = "query",required = true),
            @ApiImplicitParam(name = "deposit", value = "房屋押金",paramType = "query",required = true)
    })
    @PostMapping("/saveOrUpdatePrince")
    public Result saveOrUpdatePrince(Integer id, @NotNull String description, @NotNull String day,@NotNull String month,@NotNull String year,@NotNull String deposit){
        Prince prince=new Prince();
        prince.setId(id);
        prince.setDescription(description);
        prince.setDay(day);
        prince.setMonth(month);
        prince.setYear(year);
        prince.setDeposit(deposit);
        roomService.saveOrUpdatePrince(prince);
        return Result.success("插入或更新成功");
    }
    @ApiOperation(value = "删除房屋")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id",value = "主键",paramType = "query",required=true)
    )
    @GetMapping("/deleteByIdRoom")
    public Result deleteByIdRoom(@NotNull Integer id) {
        roomService.deleteByIdRoom(id);
        return Result.success("删除成功");
    }
    @ApiOperation(value = "分页查询房屋具体信息")
    @GetMapping("/pageQuery")
    @ApiImplicitParams({ @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "roomId", value = "房间号", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态,包含未租，已租等状态", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "房屋类型id", paramType = "query")
    })
    public Result pageQuery(@NotNull Integer page, @NotNull Integer pageSize, Integer roomId, String status,Integer type) {
        PageVM<RoomExtend> pageVM = roomService.query(page,pageSize,roomId,status,type);
        return Result.success(pageVM);
    }
    @ApiOperation(value = "查询所有房屋类型具体信息")
    @GetMapping("/queryPrinces")
    public Result queryPrinces() {
        List<Prince> list=roomService.queryPrinces();
        return Result.success(list);
    }
    @ApiOperation(value = "根据id查询房屋信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id",value = "主键",paramType = "query",required=true)
    )
    @GetMapping("/queryById")
    public Result queryById(@NotNull Integer id) {
        RoomExtend roomExtend=roomService.queryById(id);
        return Result.success(roomExtend);
    }

    @ApiOperation(value="将用户信息导入到Excel中",notes="注意！测试的时候请将地址粘贴到浏览器地址栏测试",produces="application/octet-stream")
    @GetMapping(value = "download")
    public void download(HttpServletResponse response) throws Exception{
        // 查询出所有文章信息
        String excelName = "房屋信息记录表";
        String[] headList = new String[]{"编号","房间类型","租住状态","位置"};
        String[] fieldList = new String[]{"id","backup1","status","backup2"};
        List<Map<String, Object>> dataList = new ArrayList<>();
        List<Room> list =roomService.findAll();
        for(Room a : list){
            Map<String, Object> map = new HashMap<>();
            map.put("id",a.getRoomId());
            map.put("backup1",a.getBackup1());
            map.put("status",a.getStatus());
            map.put("backup2",a.getBackup2());

            dataList.add(map);
        }

        //调用工具类导出excel
        ExcelUtils.createExcel(response,excelName,headList,fieldList,dataList);

    }
}
